<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card shadow="hover" :body-style="{ height: '550px' }">
          <div class="card-header">
            <p class="card-header-title">文件缺陷定位历史</p>
            <p class="card-header-desc">项目最近文件定位情况</p>
          </div>
          <el-table
              :data="RecommendedFileList"
              border
              max-height="500"
              size=small
              class="file-table"
              style="margin-bottom: 10px"
          >
            <!--          展示一栏序号-->
            <el-table-column label="序号" type="index" width="50" style="align-items: center"></el-table-column>
            <el-table-column label="项目名称" prop="project_name"
                             style="width: auto;align-items: center"></el-table-column>
            <el-table-column label="文件名称" prop="file_name"
                             style="width: auto; align-items: center"></el-table-column>
            <el-table-column label="文件位置" prop="file_path"
                             style="width: auto; align-items: center"></el-table-column>
            <el-table-column label="可疑分数" prop="score" style="width: auto; align-items: center"></el-table-column>
          </el-table>


        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover" :body-style="{ height: '550px' }">
          <div class="card-header">
            <p class="card-header-title">语句缺陷定位历史</p>
            <p class="card-header-desc">项目最近语句定位情况</p>
          </div>
          <!--        展示可疑文件列表以及分数-->
          <el-table
              :data="RecommendedSenList"
              border
              max-height="500"
              size=small
              class="file-table"
              style="margin-bottom: 10px"
          >
            <!--          展示一栏序号-->
            <el-table-column label="序号" type="index" width="50" style="align-items: center"></el-table-column>
            <el-table-column label="项目名称" prop="project_name"
                             style="width: auto;align-items: center"></el-table-column>
            <el-table-column label="文件位置" prop="file_path"
                             style="width: auto; align-items: center"></el-table-column>
            <el-table-column label="语句位置" prop="sentence"
                             style="width: auto; align-items: center"></el-table-column>
            <el-table-column label="可疑分数" prop="score" style="width: auto; align-items: center"></el-table-column>
          </el-table>

        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts" name="dashboard">
import {onMounted, ref} from "vue";
import {ElMessage} from 'element-plus'
import axios from "axios";

const globalApiUrl = import.meta.env.VITE_API_URL

console.log('Global API URL:', globalApiUrl);
let RecommendedFileList = ref([]); // 推荐列表
let RecommendedSenList = ref([]); // 推荐列表

// 获取最新的推荐列表
const getRecommendedFileList = async () => {
  const preprocessUrl = globalApiUrl + '/history';
  try {
    const response = await axios.get(preprocessUrl, {
      headers: {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer ' + localStorage.getItem('access_token')
      },
      params: {
        type: 'file'
      }
    });
    if (response.data.result) {
      RecommendedFileList.value = response.data.data
    } else {
      ElMessage.error("获取推荐列表失败，请联系管理员")
    }
  } catch (error) {
    ElMessage.error("获取推荐列表失败，请稍后再试")
  }
}
// 获取最新的推荐列表
const getRecommendedSenList = async () => {
  const preprocessUrl = globalApiUrl + '/history';
  try {
    const response = await axios.get(preprocessUrl, {
      headers: {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer ' + localStorage.getItem('access_token')
      },
      params: {
        type: 'sen'
      }
    });
    if (response.data.result) {
      RecommendedSenList.value = response.data.data
    } else {
      ElMessage.error("获取推荐列表失败，请联系管理员")
    }
  } catch (error) {
    ElMessage.error("获取推荐列表失败，请稍后再试")
  }
}
onMounted(async () => {
  await getRecommendedFileList();
  await getRecommendedSenList();
});
</script>

<style>

.card-header {
  padding-left: 10px;
  margin-bottom: 20px;
}

.card-header-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.card-header-desc {
  font-size: 14px;
  color: #999;
}

</style>
